<template>

    <!-- 添加或修改对话框 -->
    <lam-dialog :title="title" :visible.sync="open" width="680px" append-to-body @close="cancel">
      <el-form :ref="formRef" :model="form" :rules="rules" label-width="100px" :disabled="formDisabled">
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目模块" prop="projectModule">
              <span slot="label">
                项目模块<el-tooltip content="项目工程模块，例如 lam-system" placement="top"><i class="el-icon-question"></i></el-tooltip>
              </span>
              <lam-dict-select v-model="form.projectModule" dictCode="project_module" placeholder="" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="代码模块" prop="module">
              <span slot="label">
                代码模块<el-tooltip content="代码模块，例如 system、monitor" placement="top"><i class="el-icon-question"></i></el-tooltip>
              </span>
              <el-input v-model="form.module" clearable placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="模块简写" prop="moduleShort">
              <span slot="label">
                模块简写<el-tooltip content="代码模块简写，可以和代码模块一样，例如 system=>sys、monitor=>monitor" placement="top"><i class="el-icon-question"></i></el-tooltip>
              </span>
              <el-input v-model="form.moduleShort" clearable placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="模块描述" prop="moduleName">
              <el-input v-model="form.moduleName" clearable placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="表前缀" prop="tablePrefix">
              <span slot="label">
                表前缀<el-tooltip content="该代码模块中包含的表功能的表的前缀，多个用英文逗号分隔，例如 sys_,gen_" placement="top"><i class="el-icon-question"></i></el-tooltip>
              </span>
              <el-input v-model="form.tablePrefix" clearable placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <lam-dict-select type="radio" v-model="form.status" dictCode="status_type" placeholder="" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="包名" prop="packageName">
              <span slot="label">
                包名<el-tooltip content="该代码模块的基础包名，例如 system模块=>com.lam.system" placement="top"><i class="el-icon-question"></i></el-tooltip>
              </span>
              <el-input v-model="form.packageName" clearable placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="UI组件位置" prop="uiPath">
              <span slot="label">
                UI组件位置<el-tooltip content="该代码模块的界面UI组件代码的位置，例如 system模块=>system/" placement="top"><i class="el-icon-question"></i></el-tooltip>
              </span>
              <el-input v-model="form.uiPath" type="textarea" autosize placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" autosize placeholder="" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :loading="loading">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </lam-dialog>

</template>

<script>
import { EditMixin } from '@/components/mixins/EditMixin';

export default {
  name: "EditCodeModule",
  mixins:[EditMixin],
  data() {
    return {
      url: {
        base: '/tool/codeModule'
      },
      // 表单校验
      rules: {
        projectModule: [{ required: true, message: "项目模块不能为空", trigger: "blur" }],
        module: [{required: true, message: "模块不能为空", trigger: "blur" }, {validator: this.moduleUniqueValidator, trigger: 'blur'}],
        moduleShort: [{ required: true, message: "模块简写不能为空", trigger: "blur" }],
        moduleName: [{ required: true, message: "模块名不能为空", trigger: "blur" }],
        packageName: [{ required: true, message: "包名不能为空", trigger: "blur" }],
        tablePrefix: [{ required: true, message: "表前缀不能为空", trigger: "blur" }],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
  },
  watch:{
    'form.module':function(newVal, oldVal){
      if(newVal && this.operType=='add'){
        if(!this.form.moduleShort || this.form.moduleShort==oldVal){
          this.form.moduleShort = newVal;
        }
        if(!this.form.packageName || this.form.packageName==('com.lam.'+oldVal)){
          this.form.packageName = 'com.lam.'+newVal;
        }

      }
    }
  },
  methods: {
    // 表单重置
    reset() {
      this.form = {
        projectModule: null,
        module: null,
        moduleShort: null,
        moduleName: null,
        packageName: null,
        tablePrefix: null,
        uiPath: null,
        status: 1,
        remark: null
      };
    },

    moduleUniqueValidator(rule, value, callback) {
      let param = {module: value};
      if(this.operType=='update') param.id = this.form.id;
      this.checkUniqueAsyn(value, callback, 'codeModule', param);
    },

    /** 提交按钮 */
    submitForm(){
      this.saveForm(this.url.base);
    }

  }
};
</script>
